extends CharacterBody2D
class_name Player

@onready var velocity_component: Node = $VelocityComponent
var interacting_with : Array[Interactable]

@onready var back: Node2D = $Back


func _physics_process(delta: float) -> void:
	
	var movement_vector = get_movement_vector()
	var direction = movement_vector.normalized()
	
	velocity_component.accelerate_in_direction(direction)
	
	if movement_vector.y < 0 :
		back.visible = true
		var anim = get_tree().get_first_node_in_group("animation")
		print(anim)
		anim.play("walk")
		
		
	
	

func get_movement_vector() -> Vector2:
	var x_movement = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
	var y_movement = Input.get_action_strength("move_down") - Input.get_action_strength("move_up")
	return Vector2(x_movement, y_movement)


func _unhandled_input(event: InputEvent) -> void:
	if event.is_action_pressed("interact") and interacting_with:
		interacting_with.back().interact()#死了还要清空
		

func register_interactable(v: Interactable) -> void:#注册一个实例数组中的成员
	if v in interacting_with:
		return
	interacting_with.append(v)
	
func unregister_interactable(v: Interactable) -> void:#取消注册
	interacting_with.erase(v)
